<template>
  <a-modal
    style="width: 50%"
    title="修改账号密码"
    :visible="visible"
    :confirm-loading="confirmLoading"
    :destroyOnClose="true"
    @cancel="handleCancel"
    :maskClosable="false">
    <div style="margin-top: 12px;min-height: 300px;">
      <a-form-model ref="changeForm" :model="addForm" :rules="addRules" >
        <a-form-model-item label="旧密码" prop="oldPassword" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-input-password v-model.trim="addForm.oldPassword" type="text" placeholder="请输入旧密码" />
        </a-form-model-item>
        <a-form-model-item label="新密码" prop="newPassword" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-input-password v-model.trim="addForm.newPassword" type="text" placeholder="请输入新密码" />
        </a-form-model-item>
        <a-form-model-item label="确认新密码" prop="confirmPassword" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-input-password v-model.trim="addForm.confirmPassword" type="text" placeholder="请输入确认新密码" />
        </a-form-model-item>
      </a-form-model>
    </div>
    <template slot="footer">
      <a-button key="back" @click="handleCancel">
        关闭
      </a-button>
      <a-button type="primary" :visible="visibleBtn" :loading="loadingBtn" @click="handleOk">
        保存
      </a-button>
    </template>
  </a-modal>
</template>
<script>
export default {
    data () {
        return {
            addRules: {
                oldPassword: [
                    { required: true, message: '请输入旧密码!', trigger: 'blur' }
                ],
                newPassword: [
                    { required: true, message: '请输入新密码!', trigger: 'blur' }
                ],
                confirmPassword: [
                    { required: true, message: '请输入确认新密码!', trigger: 'blur' }
                ]
            },
            addForm: {
                oldPassword: '',
                newPassword: '',
                confirmPassword: ''
            },
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 }
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 }
            },
            visible: false,
            confirmLoading: false,
            loading: false,
            visibleBtn: false,
            loadingBtn: false

        }
    },
    methods: {
        // 显示
        open () {
            this.visible = true
            return this
        },
        // 关闭
        handleCancel () {
            this.visible = false
            this.loadingBtn = false
            this.visibleBtn = false
            this.$emit('close')
        },
        // 确认
        handleOk () {
            this.loadingBtn = true
            this.visibleBtn = true
            this.$refs.changeForm.validate(valid => {
                if (valid) {
                    this.$ChangePwd(this.addForm).then(res => {
                        if (res.code === 200) {
                            this.$notification['success']({
                                message: '提示',
                                description: '保存成功！',
                                duration: 8
                            })
                            this.$emit('success')
                            this.visible = false
                        } else {
                            this.$notification['error']({
                                message: '提示',
                                description: res.message,
                                duration: 8
                            })
                        }
                        this.visibleBtn = false
                        this.loadingBtn = false
                    })
                } else {
                    this.loadingBtn = false
                    this.visibleBtn = false
                    return false
                }
            })
        }
    }
}
</script>
<style scoped></style>
